package com.sftz.modules.minapp.homepage.service;

import java.io.IOException;
import java.io.InputStream;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.SqlManager;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.upload.TempFile;

import com.sftz.assets.entities.SUserEntity;
import com.sftz.framework.constants.CommonConstants;
import com.sftz.framework.service.qiniu.QiniuDownloadService;
import com.uxuexi.core.db.dao.IDbDao;

/**
 *
 * @author   王宏伟
 * @Date	 2017年12月29日 	 
 */
@IocBean
public class UpdataUserAvatarServcie {
	private static final Log log = Logs.get();

	@Inject
	private Dao dao;
	@Inject
	private IDbDao dbDao;
	@Inject
	private SqlManager sqlManager;
	@Inject
	private QiniuDownloadService qiniuUploadService;

	/**
	 * 	上传头像
	 * @return 
	 * @throws IOException 
	 */
	@Aop("txDb")
	public String updataUserAvatar(TempFile file, Integer userId) throws IOException {
		//获取输入流
		InputStream inputStream = file.getInputStream();
		String fileName = file.getFile().getName();
		//文件后缀
		String suffix = Files.getSuffix(fileName);
		suffix = suffix.substring(1);
		String uploadImage = qiniuUploadService.uploadImage(inputStream, suffix, fileName);
		String imgPath = CommonConstants.IMAGES_SERVER_ADDR + uploadImage;
		dbDao.update(SUserEntity.class, Chain.make("avatar", imgPath), Cnd.where("id", "=", userId));
		return imgPath;
	}
}
